package cn.edu.hqu.cst.android.ljc.anotherapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.edu.hqu.cst.android.ljc.ex4.Aidl;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    int aNumInput;
    int bNumInput;
    private Aidl remoteAidl;
    Intent intent = new Intent(MainActivity.this, Aidl.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aNumInput = R.id.numAInout;
        bNumInput = R.id.numBInput;
        findViewById(R.id.startService).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.startService:
                bindService(intent,sc,BIND_AUTO_CREATE);
                try {
                    remoteAidl.add(aNumInput,bNumInput);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                unbindService(sc);
                remoteAidl = null;

        }
    }

    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            remoteAidl = Aidl.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            remoteAidl = null;
        }
    };
}